﻿using MS_Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MS_UI
{
    public partial class Frm_NewVar_Mod : Form
    {
        public Frm_NewVar_Mod()
        {
            InitializeComponent();
            this.Load += Frm_NewVar_Mod_Load;
        }

        // 创建变量的实例
        public Variable_Modbus var;
        // 创建报警变量的实例
        public VarAlarm_Modbus varAlarm;

        private void Frm_NewVar_Mod_Load(object sender, EventArgs e)
        {
            // 初始化ComboBox下拉菜单
            this.cmb_DataType.Items.Add("Bool");
            this.cmb_DataType.Items.Add("Signed");
            this.cmb_DataType.Items.Add("Unsigned");
            this.cmb_DataType.Items.Add("Hex");
            this.cmb_DataType.Items.Add("Long");
            this.cmb_DataType.Items.Add("Long Inverse");
            this.cmb_DataType.Items.Add("Float");
            this.cmb_DataType.Items.Add("Float Inverse");
            this.cmb_DataType.Items.Add("Double");
            this.cmb_DataType.Items.Add("Double Invarse");
            this.cmb_DataType.SelectedIndex = 6;
            this.cmb_StoreArea.Items.Add("01 Coll Status(0x)");
            this.cmb_StoreArea.Items.Add("02 Input Status(1x)");
            this.cmb_StoreArea.Items.Add("03 Holding Register(4x)");
            this.cmb_StoreArea.Items.Add("04 Input Register(3x)");
            this.cmb_StoreArea.SelectedIndex = 2;

            this.chk_High.Enabled = false;
            this.chk_HiHi.Enabled = false;
            this.chk_Low.Enabled = false;
            this.chk_LoLo.Enabled = false;


        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            // Modbus变量实例
            var = new Variable_Modbus();
            var.VarName = this.txt_VarName.Text.Trim();
            var.StoreArea = cmb_StoreArea.Text.Trim();
            var.DataType = this.cmb_DataType.Text.Trim();
            var.Address = this.txt_Address.Text.Trim();
            var.IsFilling = this.chk_IsFiling.Checked ? "1" : "0";
            var.IsAlarm = this.chk_IsAlarm.Checked ? "1" : "0";
            var.IsReport = this.chk_IsReport.Checked ? "1" : "0";
            var.Note = this.txt_Note.Text.Trim();
            // Modbus变量报警实例
            if (this.chk_High.Checked | this.chk_HiHi.Checked | this.chk_LoLo.Checked|this.chk_Low.Checked)
            {
                varAlarm = new VarAlarm_Modbus();
                varAlarm.VarName = this.txt_VarName.Text.Trim();
                if (chk_High.Checked)
                {
                    varAlarm.VarAlarm.Add
                        (new Alarm()
                        {
                            Priorty = Convert.ToInt32(this.txt_Priority_High.Text.Trim()),
                            AlarmType = "High",
                            AlarmValue = float.Parse(this.txt_Alarm_High.Text.Trim()),
                            Note = this.txt_Note_High.Text.Trim()
                        }) ;
                }

                if (chk_HiHi.Checked)
                {
                    varAlarm.VarAlarm.Add
                        (new Alarm()
                        {
                            Priorty = Convert.ToInt32(this.txt_Priority_HiHi.Text.Trim()),
                            AlarmType = "HiHi",
                            AlarmValue = float.Parse(this.txt_Alarm_HiHi.Text.Trim()),
                            Note = this.txt_Note_HiHi.Text.Trim()
                        });
                }

                if (chk_Low.Checked)
                {
                    varAlarm.VarAlarm.Add
                        (new Alarm()
                        {
                            Priorty = Convert.ToInt32(this.txt_Priority_Low.Text.Trim()),
                            AlarmType = "Low",
                            AlarmValue = float.Parse(this.txt_Alarm_Low.Text.Trim()),
                            Note = this.txt_Note_Low.Text.Trim()
                        });
                }
                if (chk_LoLo.Checked)
                {
                    varAlarm.VarAlarm.Add
                        (new Alarm()
                        {
                            Priorty = Convert.ToInt32(this.txt_Priority_LoLo.Text.Trim()),
                            AlarmType = "LoLo",
                            AlarmValue = float.Parse(this.txt_Alarm_LoLo.Text.Trim()),
                            Note = this.txt_Note_LoLo.Text.Trim()
                        });
                }
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        #region CheckBox变化事件
        private void chk_IsAlarm_CheckedChanged(object sender, EventArgs e)
        {
            bool value = this.chk_IsAlarm.Checked;
            this.chk_High.Enabled = value;
            this.chk_HiHi.Enabled = value;
            this.chk_Low.Enabled = value;
            this.chk_LoLo.Enabled = value;
        }

        private void chk_LoLo_CheckedChanged(object sender, EventArgs e)
        {
            bool value = this.chk_LoLo.Checked;
            txt_Alarm_LoLo.Enabled = value;
            txt_Priority_LoLo.Enabled = value;
            txt_Note_LoLo.Enabled = value;
        }

        private void chk_Low_CheckedChanged(object sender, EventArgs e)
        {
            bool value = this.chk_Low.Checked;
            txt_Alarm_Low.Enabled = value;
            txt_Priority_Low.Enabled = value;
            txt_Note_Low.Enabled = value;
        }

        private void chk_HiHi_CheckedChanged(object sender, EventArgs e)
        {
            bool value = this.chk_HiHi.Checked;
            txt_Alarm_HiHi.Enabled = value;
            txt_Priority_HiHi.Enabled = value;
            txt_Note_HiHi.Enabled = value;
        }

        private void chk_High_CheckedChanged(object sender, EventArgs e)
        {
            bool value = this.chk_High.Checked;
            txt_Alarm_High.Enabled = value;
            txt_Priority_High.Enabled = value;
            txt_Note_High.Enabled = value;
        }
        #endregion
    }
}
